<template>
  <div>
    <van-nav-bar class="top"
  title="账号登录"
  left-arrow
  @click-left="$router.push('/mine')"
/>
 <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"
    placeholder="请输入账号"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    placeholder="请输入密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button  block type="info" native-type="submit">登录</van-button>
  </div>
</van-form>
   <router-link class="link" to="/register">还没有账号，去注册～</router-link>

  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'login-page',
  components: {},
  props: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  methods: {
    async   onSubmit (val) {
      // console.log('submit', val)
      const res = await login(val)
      // console.log(res)
      if (res.status === 200) {
        this.$toast.success(res.description)
        setToken(res.body.token)
        this.$router.push('/')
      } else {
        this.$toast.fail(res.description)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.top {
  background-color: #56b37b;
    /deep/ .van-nav-bar__title {
    color: #fff;
  }
  /deep/.van-icon-arrow-left {
    color: #fff;
  }

}
.link {
  color: gray;
  font-size: 12px;
  margin-left: 35%;
}

</style>
